package com.winning.devops.batch.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 示例Job 配置
 * 顺序执行，条件判断
 *
 * @author chensj
 * @date 2020-02-28 12:13
 */
@Configuration
@EnableBatchProcessing
public class JobDemoConfig {

    /**
     * 创建Job的bean
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * job 由step组成
     * 创建Step 对象的bean
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 创建Job {@code demoJob}
     *
     * @return {@link org.springframework.batch.core.Job}
     */
    @Bean
    public Job demoJob() {

        return jobBuilderFactory
                .get("demoJob")
                // 顺序执行
                //.start(step1())
                //.next(step2())
                //.next(step3())
                //.build();
                // 开始步骤
                .start(step1())
                // on 指定条件 step1 成功状态 才会继续向下
                .on("COMPLETED")
                // to 到那个任务 step2
                .to(step2())
                // from 执行那个step
                .from(step2())
                // 判断执行条件
                .on("COMPLETED")
                // 到那个任务 step3
                .to(step3())
                // fail 失败
                // stopAndRestart 停止重启step
                // 执行那个step3
                .from(step3())
                // 结束 job
                .end()
                // 创建
                .build();
    }

    /**
     * 创建Step 1
     *
     * @return {@link org.springframework.batch.core.Step}
     */
    @Bean
    public Step step1() {
        return stepBuilderFactory
                .get("step1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("execute step1 ");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    /**
     * 创建Step 2
     *
     * @return {@link org.springframework.batch.core.Step}
     */
    @Bean
    public Step step2() {
        return stepBuilderFactory
                .get("step2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("execute step2 ");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    /**
     * 创建Step 3
     *
     * @return {@link org.springframework.batch.core.Step}
     */
    @Bean
    public Step step3() {
        return stepBuilderFactory
                .get("step3")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("execute step3 ");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }
}
